package com.dongdavid.imageupload;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.ApplicationInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private Button btn;
    private ImageView iv;
    private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/jpeg");
    private final OkHttpClient client = new OkHttpClient();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

    }

    private void init() {
        btn = (Button) findViewById(R.id.button2);
        iv = (ImageView) findViewById(R.id.imageView);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("click");
                Log.i("ddv", "click");
                btn.setText("你有本事点我啊");
                iv.setImageResource(R.drawable.jt2);
                GetByHttpClient();
            }
        });

    }

    private File getfile(){
        @SuppressLint("ResourceType") InputStream is = getResources().openRawResource(R.drawable.jt2);
        Bitmap bitmap = BitmapFactory.decodeStream(is);

        String defaultPath = getApplicationContext().getFilesDir().getAbsolutePath();
        Log.i("ddv","defaultpath"+defaultPath);
        File file = new File(defaultPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        String defaultImgPath = defaultPath + "/messageImg.jpg";
        file = new File(defaultImgPath);
        try {
            file.createNewFile();
            FileOutputStream fOut = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 20, fOut);
            is.close();
            fOut.flush();
            fOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }
    private void GetByHttpClient() {
        System.out.println("click");
        Log.i("ddv", "http");
        new Thread()
        {
            public void run()
            {
                OkHttpClient okHttpClient = new OkHttpClient.Builder()
                        .build();
                File file = getfile();
                Log.i("ddv", "getfile");
                RequestBody fileBody = RequestBody.create(MEDIA_TYPE_PNG, file);
                RequestBody requestBody = new MultipartBody.Builder()
                        .setType(MultipartBody.FORM)
                        .addFormDataPart("file", "jt2.jpg", fileBody)
                        .build();

                //4.构建请求
                Request request = new Request.Builder()
                        .url("https://www.xxx.com/upload/xxx")
                        .post(requestBody)
                        .build();

                //5.发送请求
                try {
                    Response response = client.newCall(request).execute();
                    Log.i("ddv",response.body().string());

                } catch (IOException e) {
                    e.printStackTrace();
                    Log.i("ddv", e.toString());

                }

            };
        }.start();
    }

}